/*******************************************************************
 *
 * Copyright (C), 2021-2022, LS, All rights reserved.
 *
 * Author      : 
 * Create Time : 2021-6-21
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "wm.h"
#include "stdio.h"
#include "string.h"
#include "app_reset.h"
#include "app_display.h"
#include "app_battery.h"
#include "app_skip_alg.h"
#include "app_global_data.h"
#include "app_store_set_info.h"
#include "user_config.h"

#if(DEV_MAC_BURN_ENABLE)

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/
extern void skip_adv_data_1hz_update(void);

/*-----------------------------------------------------------------*/

void lsb_msb_exchange(uint8_t *pData, uint8_t size)
{
	uint8_t tmp, i;
	
	for(i=0; i<(size/2); ++i) {
	    tmp = pData[i];
	    pData[i] = pData[size-1-i];
	    pData[size-1-i] = tmp;
	}
}

void hex_to_ascii(uint8_t *pDest, uint8_t *pSrc, uint8_t ucLen)
{
	uint8_t msb, lsb;
	for(uint8_t i = 0; i < ucLen; i++) {
		msb = (*pSrc >> 4);
		lsb = (*pSrc & 0x0F);
		if(msb > 0x09) {
			*pDest = msb + 'A' -0x0a;
		}
		else {
			*pDest = msb + '0';
		}
		pDest++;

		if(lsb > 0x09) {
			*pDest = lsb + 'A' -0x0a;
		}
		else {
			*pDest = lsb + '0';
		}
		pDest++;
		pSrc++;
	}
}

static void wm_sn_disp_short_press_cb(void)
{
    app_disp_img_all_clear();
    ui_revert_last_skip_mode_disp(false);
    if(true == app_batt_charge_state_get()) {
        wm_batt_charging_disp();
    }
}


uint32_t mac_num_convert(uint8_t *mac)
{
    uint8_t num[8];
    uint32_t mac_num = 0;
    
    num[0] = mac[3]/0x10;
    num[1] = mac[3]%0x10;
    num[2] = mac[2]/0x10;
    num[3] = mac[2]%0x10;
    num[4] = mac[1]/0x10;
    num[5] = mac[1]%0x10;
    num[6] = mac[0]/0x10;
    num[7] = mac[0]%0x10;
    
    mac_num = num[0]*10000000;
    mac_num += num[1]*1000000;
    mac_num += num[2]*100000;
    mac_num += num[3]*10000;
    mac_num += num[4]*1000;
    mac_num += num[5]*100;
    mac_num += num[6]*10;
    mac_num += num[7];
    
    return mac_num;
}


void wm_sn_disp(void)
{
    wm_t wm;

    memset(&wm, 0, sizeof(wm));
    wm.cur_win = SN_WIN;
    wm.revert_win = wm_get_revert_win();
    wm.disp_off_cnt = DISP_NEVER_OFF_CNT;
    wm.auto_update_1hz_cb = skip_adv_data_1hz_update;
    wm.short_press_cb = wm_sn_disp_short_press_cb;

    wm_info_set(&wm);

    //app_skip_mode_set(SKIP_MODE_NULL);

    //for test
    //uint8_t mac1[DEV_MAC_LEN] = {0x02, 0x08, 0x00, 0x00, 0xA0, 0xFC};
    //app_set_info_burn_mac_save(mac1, sizeof(mac1));
    //#include "hal_flash.h"
    //hal_nvr_erase_sector(BURN_INFO_START_ADDR);

    app_disp_img_all_clear();
    
    uint8_t mac[DEV_MAC_LEN];
    if(DEV_MAC_LEN == app_set_info_burn_mac_get(mac))
    {
        app_disp_mac_num(75, 8, mac_num_convert(mac));
        
        char str[41];
        uint8_t sn_str[DEV_MAC_LEN*2+1];
        memset(str, 0, sizeof(str));
        memset(sn_str, 0, sizeof(sn_str));
        lsb_msb_exchange(mac, DEV_MAC_LEN);
        hex_to_ascii(sn_str, mac, DEV_MAC_LEN);
#if(SCHOOL_SKIP_ENABLE)
        sn_str[DEV_MAC_LEN*2] = '\0';
        sprintf(str, "https://q.suredoc.cn/?m=%s&t=2", sn_str);
        app_disp_qrcode_draw(5, 10, 2, str);   //FCA000000123  //https://q.suredoc.cn/?m=FCA000000224&t=2
#endif
    }
    else {
        app_disp_user_head_icon(15, 22);
    }

    wm_set_backlight_off_cnt(DISP_NEVER_OFF_CNT);
    app_disp_backlight_set(true);
}


#endif
